package distribution;

import java.util.ArrayList;
import java.util.List;

public class LowerDS {

	public static Integer[] RED = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
			31, 32, 33 };
	public static Integer[] BLUE = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
			14, 15, 16 };

	/**
	 * 
	 * @param optionalCodes
	 * @param optionalCount
	 * @param forceCodes
	 * @param forceCount
	 * @return
	 */
	public static List<SchemeItem> allCombination(List<Integer> optionalCodes,
			int optionalCount, List<Integer> forceCodes, int forceCount) {
		List<SchemeItem> result = new ArrayList<SchemeItem>();

		Combination<Integer> oc = new Combination<Integer>(optionalCodes);
		List<List<Integer>> ol = oc.allCombination(optionalCount);

		Combination<Integer> fc = new Combination<Integer>(forceCodes);
		List<List<Integer>> fl = fc.allCombination(forceCount);

		if (ol != null && fl != null) {
			if (ol.size() > 0 && fl.size() > 0) {
				for (List<Integer> o : ol) {
					for (List<Integer> f : fl) {
						List<Integer> mr = Combination.merge(o, f);
						SchemeItem si = new SchemeItem(mr);
						result.add(si);
					}
				}
			} else if (fl.size() > 0) {
				for (List<Integer> f : fl) {
					List<Integer> mr = Combination.merge(null, f);
					SchemeItem si = new SchemeItem(mr);
					result.add(si);
				}
			} else if (ol.size() > 0) {
				for (List<Integer> o : ol) {
					List<Integer> mr = Combination.merge(o, null);
					SchemeItem si = new SchemeItem(mr);
					result.add(si);
				}
			}
		}

		return result;
	}

	public static List<SchemeItem> allCombination(List<Integer> codes, int count) {
		List<SchemeItem> result = new ArrayList<SchemeItem>();
		Combination<Integer> fc = new Combination<Integer>(codes);
		List<List<Integer>> fl = fc.allCombination(count);

		System.out.println(fl.size());
		for (List<Integer> f : fl) {
			SchemeItem si = new SchemeItem(f);
			result.add(si);
		}
		return result;
	}

	public static void main(String[] args) {
		List<Integer> all = Combination.asList(RED);
		List<Integer> code = Combination.asList(new Integer[] { 1, 7, 8, 15,
				19, 31 });
		List<Integer> sub = Combination.substract(all, code);

		// List<Integer> code = Combination.asList(RED);
		// Combination<Integer> cc = new Combination<Integer>(code);
		// cc.multipThreadAllCombination(6, null, 3);
		// System.out.println(cc.getResult().size());
		// System.out.println(cc.getResult());

		List<SchemeItem> sl = allCombination(sub, 6);
		Combination<SchemeItem> csi = new Combination<SchemeItem>(sl);
		List<List<SchemeItem>> result = csi.multipThreadAllCombination(5, 10,
				new CombinationFilter<SchemeItem>() {
					@Override
					public boolean check(List<SchemeItem> data) {
						if (data != null) {
							long ret = 0;
							for (SchemeItem si : data) {
								ret |= si.getBitCode();
							}
							if (Long.bitCount(ret) == 30) {
								return true;
							}
						}
						return false;
					}
				});
		System.out.println(result.size());
	}

}
